<?php
class Tintuyendung extends MY_Controller {
    /**
    * Responsable for auto load the model
    * @return void
    */
   public function __construct(){
		parent::__construct();
		$this->load->model('tuyendung_m','tuyendung');
   }
	
	function index($id = false){
		$data = array();
		$tin = $this->tuyendung->get_tin_da_duyet($id);
		// echo '<pre>';print_r($tin);die;
		if(!$tin)
			show_404();
		$data['tin']	=	$tin;		
		$dichvu	= explode(',',$tin->dich_vu);
		if(!$dichvu) $dichvu = array();
		$data['related'] = $this->tuyendung->get_related($dichvu, $tin->id);
		// echo $this->db->last_query();die;	
		$user = $this->session->userdata('userdata');
		$data['is_apply']= (bool)$this->tuyendung->is_apply($user['canhan_id'], $tin->id, $tin->ntd_id);		
		// var_dump($data['is_apply']);die;		
		$data['page_title']	=	$tin->tieu_de;
		$data['title'] 	= 'Tin tuyển dụng - '.$tin->tieu_de;
		//FB
		$data['og_title']	= $tin->tieu_de . ' - pgvietnamworks.com';
		$data['og_description']	= $tin->mo_ta . ' ' .$tin->yeu_cau;		
		$data['og_image']	= 'http://pgvietnamworks.com/assets/img/logo4fb.png';
		//
		$data['content'] 	= 'tuyendung/xemtintuyendung';
		$data['quangcao'] = 'tpl/quangcao';
		$this->load->view('tpl/template', $data);
	}
	
	function apply(){
		$cnId		= $this->input->post('cnId');
		$tinId	= $this->input->post('tinId');
		$ntdId	= $this->input->post('ntdId');
		$user = $this->session->userdata('userdata');
		$res	= array();
		$res['status']	=	'fail';
		$res['message']=	'Nộp đơn không thành công. Vui lòng thử lại sau!';
		if(isset($user['canhan_id']) && $user['canhan_id'] == $cnId && $tinId && $ntdId){
			$tin = $this->tuyendung->get_tin_da_duyet($tinId, $ntdId);
			if($tin){
				$obj = array();
				$obj['ntd_id']	=	$ntdId;
				$obj['cn_id']	=	$cnId;
				$obj['ttd_id']	=	$tinId;
				$obj['ngay_nop'] = date('Y-m-d H:i:s');
				$this->tuyendung->apply($obj);
				$res['status']	=	'success';
				$res['message']=	'Nộp đơn thành công.';
				$ntdObj	=	$this->tuyendung->get_email($ntdId);
				if($ntdObj){
					$this->load->library('email');
					$user	=	$this->session->userdata('userdata');
					$tin	=	$this->tuyendung->get_tin($tinId);
					$sent	=	$this->email->send_mail(array(
						'slug'	=>	'nop-don-tuyen-dung',
						'to'		=>	$ntdObj->email,
						'ho_ten'	=>	$ntdObj->ten,
						'ten_pg'	=>	$user['ho_ten'],
						'tin_link'	=>	site_url('tuyen-dung/ho-so-da-nop')
					));
					$res['send_mail']	=	$sent;
				}
			}
		}			
		echo json_encode($res);
		die;
	}
 }